package com.quanyue.music.system.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.music.domain.entity.HotStyle;
import com.quanyue.music.system.mapper.HotStyleMapper;
import com.quanyue.music.system.service.HotStyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HotStyleServiceImpl implements HotStyleService {
    @Autowired
    private HotStyleMapper hotStyleMapper;

    @Override
    public void add(HotStyle hotStyle) {
        List<HotStyle> hotStyleList = hotStyleMapper.findAllValid();
        if (hotStyleList.size() == 3) throw new AssertUtils.ValidateException("当前版本峰值固定三个");
        hotStyle.setDelFlag(false);
        hotStyle.setCreateTime(LocalDateTime.now());
        hotStyle.setModifyTime(LocalDateTime.now());
        hotStyleMapper.insert(hotStyle);
    }

    @Override
    public void update(HotStyle hotStyle) {
        HotStyle checkStyle = hotStyleMapper.findOne(hotStyle.getId());
        AssertUtils.notNull(checkStyle, "没有该规则");
        hotStyle.setCreateTime(checkStyle.getCreateTime());
        hotStyle.setModifyTime(LocalDateTime.now());
        hotStyle.setDelFlag(false);
        hotStyleMapper.update(hotStyle);
    }

    @Override
    public void delete(String id) {
        HotStyle hotStyle = new HotStyle();
        hotStyle.setDelFlag(true);
        hotStyle.setModifyTime(LocalDateTime.now());
//        TODO 1.0版本不允许删除
//        hotStyleMapper.updateSelective(hotStyle);
    }
}
